package cn.zhf.shop.service.impl;

import cn.hutool.json.JSONUtil;
import cn.zhf.shop.entity.*;
import cn.zhf.shop.entity.lesson.Lesson;
import cn.zhf.shop.entity.organization.Organization;
import cn.zhf.shop.entity.search.CampusSearch;
import cn.zhf.shop.mapper.CampusMapper;
import cn.zhf.shop.mapper.ConsoleUserMapper;
import cn.zhf.shop.mapper.LessonMapper;
import cn.zhf.shop.mapper.OrganizationMapper;
import cn.zhf.shop.service.CampusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CampusServiceImpl implements CampusService {

    @Resource
    private CampusMapper campusMapper;

    @Resource
    private ConsoleUserMapper consoleUserMapper;

    @Resource
    private LessonMapper lessonMapper;

    @Resource
    private OrganizationMapper organizationMapper;


    private Logger logger= LoggerFactory.getLogger(getClass());

    @Override
    public Message<?> addCampus(Campus c) {
        logger.info("Add new campus name:{},",c.getCampusName());
        Date date = new Date();
        c.setCreateTime(date);
        if(campusMapper.insert(c)>0)
        {
            ConsoleUser consoleUser = consoleUserMapper.selectByPrimaryKey(c.getHead());
            consoleUser.setState(1);
            return consoleUserMapper.updateByPrimaryKeySelective(consoleUser)>0 ? Message.success(null):Message.fail(null);
        }else {
            return Message.fail("添加失败");
        }
    }

    @Override
    public Message<Campus> getCampusById(Integer id) {
        logger.info("select campus by id,id:{}",id);
        return Message.success(null).add(campusMapper.selectByPrimaryKey(id));
    }

    @Override
    public Message<List<Campus>> getAllCampus() {
        logger.info("select all campus");
        List<Campus> campusList = campusMapper.selectAll();
        List<Campus> campusList1 = new LinkedList<>();
        for(int i=0;i<campusList.size();i++){
            Campus campus = campusList.get(i);
            campus.setOrg_username(consoleUserMapper.selectByPrimaryKey(campus.getHead()).getUsername());
            campus.setOrgName(organizationMapper.selectByPrimaryKey(campus.getOrgId()).getOrgName());
            campusList1.add(campus);
        }
        return Message.success(null).add(campusList1);
    }

    @Override
    public Message<?> deleteCampus(Integer id) {
        logger.info("delete campus by id,id:{}",id);
        List<Lesson> lessonList =lessonMapper.selectByCampus(id);
        if(lessonList.size() == 0){
            return campusMapper.deleteByPrimaryKey(id) > 0 ? Message.success(null) : Message.fail(null);
        }else{
            return Message.fail("该条数据不能删除");
        }
    }

    @Override
    public Message<List<Campus>> getCampusByOrgId(Integer orgId) {
        logger.info("get campus by orgId,orgId:{}",orgId);
        List<Campus> campusList=campusMapper.selectByOrgId(orgId);
        List<Campus> campusList1 = new LinkedList<>();
        for(Campus campus : campusList) {
            ConsoleUser consoleUser = consoleUserMapper.selectByPrimaryKey(campus.getHead());
            campus.setOrg_username(consoleUser.getUsername());
            campus.setOrgName(organizationMapper.selectByPrimaryKey(campus.getOrgId()).getOrgName());
            campusList1.add(campus);
        }
        return  Message.success(null).add(campusList1) ;
    }

    @Override
    public Message<List<Campus>> getCampusByCity(String city) {
        logger.info("get campus by city,city:{}",city);
        return Message.success(null).add(campusMapper.selectByCity(city));
    }

    @Override
    public Message<List<Campus>> searchCampusByName(String name) {
        logger.info("search campus by name,name:{}",name);
        return Message.success(null).add(campusMapper.searchByCampusName(name));
    }

    @Override
    public Message<?> updateCampusState(Integer id, Integer state) {
        logger.info("update state by id,id:{},state:{}",id,state);
        return campusMapper.updateCampusStateByCampusId(state, id) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<?> updateCampusById(Integer id, Campus c) {
        c.setCampusId(id);
        logger.info("update campus by Id,id:{},Campus:{}",id, JSONUtil.toJsonStr(c));
        Campus campus = campusMapper.selectByPrimaryKey(c.getCampusId());
        if(campus.getHead()==0){
           ConsoleUser consoleUser =consoleUserMapper.selectByPrimaryKey(c.getHead());
            consoleUser.setState(1);
        }else{
            ConsoleUser consoleUser=consoleUserMapper.selectByPrimaryKey(campus.getHead());
            consoleUser.setState(0);
            consoleUserMapper.updateByPrimaryKeySelective(consoleUser);
            ConsoleUser consoleUser1=consoleUserMapper.selectByPrimaryKey(c.getHead());
            consoleUser1.setState(1);
            consoleUserMapper.updateByPrimaryKeySelective(consoleUser1);
        }
        return campusMapper.updateByPrimaryKeySelective(c)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message<List<Campus>> searchCampusByHead(Integer head) {
        return Message.success(null).add(campusMapper.searchByHead(head));
    }

    @Override
    public Campus getCampusById2(Integer id) {
        return campusMapper.selectByPrimaryKey(id);
    }

    @Override
    public String selectCampusNameByCampusId(Integer campusId) {
        return campusMapper.selectCampusNameByCampusId(campusId);
    }

    @Override
    public Message<List<Campus>> selectByHead(Integer head) {
        return Message.success(null).add(campusMapper.searchByHead(head));
    }

    @Override
    public Campus selectByPrimaryKey2(Integer campusId) {
        return campusMapper.selectByPrimaryKey(campusId);
    }

    @Override
    public Message<List<Campus>> search2(Integer userid,String str){
        List<Campus> campusList1 = new LinkedList<>();

        ConsoleUser consoleUser = consoleUserMapper.selectByPrimaryKey(userid);
        if(consoleUser.getUserGroup()==1){
            List<Campus> campusList = campusMapper.selectBySearch2(str);
            for(int i=0;i<campusList.size();i++){
                Campus campus = campusList.get(i);
                campus.setOrg_username(consoleUserMapper.selectByPrimaryKey(campus.getHead()).getUsername());
                campus.setOrgName(organizationMapper.selectByPrimaryKey(campus.getOrgId()).getOrgName());
                campusList1.add(campus);
            }
        }else if(consoleUser.getUserGroup()==2){
            List<Campus> campusList = campusMapper.selectBySearch4(userid,str);
            for(int i=0;i<campusList.size();i++){
                Campus campus = campusList.get(i);
                campus.setOrg_username(consoleUserMapper.selectByPrimaryKey(campus.getHead()).getUsername());
                campus.setOrgName(organizationMapper.selectByPrimaryKey(campus.getOrgId()).getOrgName());
                campusList1.add(campus);
            }
        }else if(consoleUser.getUserGroup()==3) {
            Integer orgId = consoleUser.getOrgId();
            List<Campus> campusList = campusMapper.selectBySearch6(orgId,str);
            for (Campus campus : campusList) {
                campus.setOrg_username(consoleUserMapper.selectByPrimaryKey(campus.getHead()).getUsername());
                campus.setOrgName(organizationMapper.selectByPrimaryKey(campus.getOrgId()).getOrgName());
                campusList1.add(campus);
            }
        }else if(consoleUser.getUserGroup()==4){
            Integer orgId = consoleUser.getOrgId();
            List<Campus> campusList = campusMapper.selectBySearch6(orgId,str);
            for(int i=0;i<campusList.size();i++){
                Campus campus = campusList.get(i);
                campus.setOrg_username(consoleUserMapper.selectByPrimaryKey(campus.getHead()).getUsername());
                campus.setOrgName(organizationMapper.selectByPrimaryKey(campus.getOrgId()).getOrgName());
                campusList1.add(campus);
            }
        }

        return Message.success(null).add(campusList1);
    }

    @Override
    public Message<List<Campus>> search1(Integer userid,String campusName,String provice){
        List<Campus> campusList1 = new LinkedList<>();
        ConsoleUser consoleUser = consoleUserMapper.selectByPrimaryKey(userid);
        if(consoleUser.getUserGroup()==1) {
            List<Campus> campusList = campusMapper.selectBySearch1(campusName, provice);
            for (int i = 0; i < campusList.size(); i++) {
                Campus campus = campusList.get(i);
                campus.setOrg_username(consoleUserMapper.selectByPrimaryKey(campus.getHead()).getUsername());
                campus.setOrgName(organizationMapper.selectByPrimaryKey(campus.getOrgId()).getOrgName());
                campusList1.add(campus);
            }
        }else if(consoleUser.getUserGroup()==2){
            List<Campus> campusList = campusMapper.selectBySearch3(campusName, provice ,userid);
            for (int i = 0; i < campusList.size(); i++) {
                Campus campus = campusList.get(i);
                campus.setOrg_username(consoleUserMapper.selectByPrimaryKey(campus.getHead()).getUsername());
                campus.setOrgName(organizationMapper.selectByPrimaryKey(campus.getOrgId()).getOrgName());
                campusList1.add(campus);
            }
        }else if(consoleUser.getUserGroup()==3){
            Integer orgId = consoleUser.getOrgId();
            List<Campus> campusList = campusMapper.selectByOrgId(orgId);
            for (Campus campus : campusList) {
                List<Campus> campusList0 = campusMapper.selectBySearch5(campusName, provice ,campus.getHead());
                for (int i = 0; i < campusList0.size(); i++) {
                    Campus campus1 = campusList.get(i);
                    campus1.setOrg_username(consoleUserMapper.selectByPrimaryKey(campus1.getHead()).getUsername());
                    campus.setOrgName(organizationMapper.selectByPrimaryKey(campus.getOrgId()).getOrgName());
                    campusList1.add(campus1);
                }
            }
        }else if(consoleUser.getUserGroup()==4){
            List<Campus> campusList = campusMapper.selectBySearch5(campusName, provice ,userid);
            for (int i = 0; i < campusList.size(); i++) {
                Campus campus = campusList.get(i);
                campus.setOrg_username(consoleUserMapper.selectByPrimaryKey(campus.getHead()).getUsername());
                campus.setOrgName(organizationMapper.selectByPrimaryKey(campus.getOrgId()).getOrgName());
                campusList1.add(campus);
            }
        }





        return Message.success(null).add(campusList1);
    }

    @Override
    public Message mohuConditionSearch(CampusSearch c) {
        List<Campus> campusList =campusMapper.selectByOrgNameAndCampusNameAndProviceAndConntactAndHeadName(c);
        for (Campus campus:campusList){
            campus.setOrg_username(consoleUserMapper.selectByPrimaryKey(campus.getHead()).getUsername());
            campus.setOrgName(organizationMapper.selectByPrimaryKey(campus.getOrgId()).getOrgName());
        }
        return Message.success(null).add(campusList);
    }

    @Override
    public Message getAllcampusByreferenceId(int referenceId) {
        List<Organization> organizationList = organizationMapper.selectByReferenceId(referenceId);
        List<HashMap> mapList = new ArrayList<>();
        for (Organization organization:organizationList){
            List<Campus> campusList = campusMapper.selectByOrgId(organization.getOrgId());
            for (Campus campus:campusList){
                HashMap hashMap = new HashMap();
                hashMap.put("campus",campus);
                hashMap.put("userName",consoleUserMapper.selectByPrimaryKey(campus.getHead()).getUsername());
                hashMap.put("orgName",organization.getOrgName());
                mapList.add(hashMap);
            }
        }
        return Message.success(null).add(mapList);
    }
}
